package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

//注销本次操作的所有session和cookie信息, 退出后跳转到登录界面
@WebServlet("/exit")
public class exit extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        //清除cookie, 跳到起始页
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if(c.getName().equals("name")) {
                    c.setMaxAge(0);
                    response.addCookie(c);
                }
                if(c.getName().equals("password")) {
                    c.setMaxAge(0);
                    response.addCookie(c);
                }
            }
        }
        session.invalidate();
        response.sendRedirect("login.jsp"); // 重定向
    }
}